package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.acticle.domain.*;
import cn.wolfcode.luowowo.acticle.query.DestinationThemeQueryObject;
import cn.wolfcode.luowowo.acticle.query.StrategyCatalogQueryObject;
import cn.wolfcode.luowowo.acticle.query.TravelQueryObject;
import cn.wolfcode.luowowo.acticle.service.*;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * 地区资源管理控制器
 */
@Controller
@RequestMapping("/destination")
public class DestinationController {
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IRegionService regionService;
    @Reference
    private IStrategyCatalogService strategyCatalogService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private ITravelService travelService;
    @Reference
    private IDestinationThemeService destinationThemeService;

    //获取热门区域
    @RequestMapping("")
    public String index(Model model, @ModelAttribute("qo")DestinationThemeQueryObject qo) {
        model.addAttribute("hotRegions", regionService.queryHotRegion());

        //themes 主题数据
        qo.setPageSize(12);
        qo.setCatalogValue(DestinationTheme.CATALOG_SUIT_ALLMONTH);
        model.addAttribute("themes1", destinationThemeService.query(qo).getList());
        qo.setCatalogValue(DestinationTheme.CATALOG_TRIP_SEASON);
        model.addAttribute("themes2", destinationThemeService.query(qo).getList());
        qo.setCatalogValue(DestinationTheme.CATALOG_TRIP_MODE);
        model.addAttribute("themes3", destinationThemeService.query(qo).getList());
        return "destination/index";
    }

    //返回区域下的地区数据的html
    @RequestMapping("/getHotDestByRegionId")
    public String index(Long regionId, Model model) {
        model.addAttribute("regionId", regionId);
        //1.获取区域下的地区(省和市)数据
        //2.按照需要的格式拼接数据
        //3.将指定格式的数据渲染到模板后返回给页面,页面完成渲染
        List<Destination> list = destinationService.getDestinationByRegionId(regionId);
        int size = list.size();
        model.addAttribute("leftDests", list.subList(0, size / 2 + 1));
        model.addAttribute("rightDests", list.subList(size / 2 + 1, size));
        return "destination/hotdestTpl";
    }

    //跳转到旅游地区页面,目的地吐司显示
    @RequestMapping("/guide")
    public String guide(@RequestParam("id") Long destId, Model model) { //id目的地id
        //toasts
        List<Destination> toasts = destinationService.getToasts(destId);
        Destination destination = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts", toasts);
        model.addAttribute("totasBoss", destination.getName()+"概况");
        //dest
        model.addAttribute("dest", destination);
        //catalogs
        List<StrategyCatalog> catalogs = strategyCatalogService.getCatalogByDestId(destId);
        model.addAttribute("catalogs", catalogs);//共享攻略目录,额外sql查询封装了攻略明细
        //strategyDetails
        model.addAttribute("strategyDetails", strategyDetailService.getDetailTop3ByDestId(destId));
        return "destination/guide";
    }

    //进入概括详情
    @RequestMapping("/surveyPage")
    public String surveyPage(Model model, @ModelAttribute("qo")StrategyCatalogQueryObject qo) {
        //toasts
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        Destination destination = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts", toasts);
        //dest
        model.addAttribute("dest", destination);
        return "destination/survey";
    }

    //获取概括详情需要数据的html
    @RequestMapping("/survey")
    public String survey(Model model, @ModelAttribute("qo")StrategyCatalogQueryObject qo) {
        //catalogs
        List<StrategyCatalog> catalogs = strategyCatalogService.getCatalogByDestId(qo.getDestId());
        model.addAttribute("catalogs", catalogs);
        //catalog
            // catalog.details

        StrategyCatalog catalog = strategyCatalogService.getCatalogByCatalogId(qo.getCatalogId());
        model.addAttribute("catalog", catalog);
        // detail.strategyContent.content
        if(catalog != null && catalog.getDetails() != null && catalog.getDetails().size() > 0){
            StrategyDetail detail = catalog.getDetails().get(0);
            StrategyContent strategyContent = strategyDetailService.getContent(detail.getId());
            detail.setStrategyContent(strategyContent);
            model.addAttribute("detail", detail);
        }
        return "destination/surveyTpl";
    }

    //目的地中游记的显示
    @RequestMapping("/travels")
    public String guide(Model model, @ModelAttribute("qo")TravelQueryObject qo) {
        model.addAttribute("pageInfo", travelService.query(qo));
        return "destination/travelTpl";
    }


    //目的地筛选页面
    @RequestMapping("/travelFilter")
    public String travelFilter(Model model, @ModelAttribute("qo")DestinationThemeQueryObject qo) {
        //主题数据
        qo.setPageSize(20);
        qo.setCatalogValue(DestinationTheme.CATALOG_SUIT_ALLMONTH);
        model.addAttribute("themes1", destinationThemeService.query(qo).getList());
        qo.setCatalogValue(DestinationTheme.CATALOG_TRIP_SEASON);
        model.addAttribute("themes2", destinationThemeService.query(qo).getList());
        qo.setCatalogValue(DestinationTheme.CATALOG_TRIP_MODE);
        model.addAttribute("themes3", destinationThemeService.query(qo).getList());

        return "/destination/travelFilter";
    }

    //返回目的地数据模板
    @RequestMapping("/destinationThemeTpl")
    public String destinationThemeTpl(Model model, @ModelAttribute("qo")DestinationThemeQueryObject qo) {
        qo.setPageSize(12);
        //dests目的地数据
        model.addAttribute("pageInfo", destinationService.getDestinationBy3Condition(qo));
        return "/destination/destinationThemeTpl";
    }

    //travelPath 旅游路线(死数据)
    @RequestMapping("/travelPath")
    public String travelPath() {
        return "/destination/travelPath";
    }

}
